#include "../../include/strategy/strategyservice.h"
#include "../../include/log/log.h"

void StrategyService::init(int clientFd, int clientEpollFd)
{
    sockFd = clientFd;
    epollFd = clientEpollFd;
}

int StrategyService::comparePrice(Coin coin, int price)
{
    if(readTimes < 3)
    {
        coinPrice->updataPrice(coin, price);
        return 0;
    }
    std::vector<int> priceAndCount = coinPrice->readPriceAndCount(coin);
    //此处需修改，需要配置购买与卖出策略，暂时只简单的买卖bnb
    if(priceAndCount[1] != 0 && (price <= priceAndCount[0] - 1.5))
    {
        return -priceAndCount[1];
    }
    else if(priceAndCount[1] == 0 && (price >= priceAndCount[0] + 1.2))
    {
        return 25;
    }
    return 0;
}

StrategyService::StrategyService()
{
    coinPrice = CoinPrice::getInstance();
}

bool StrategyService::readCoinsPrice(const json& readJson, json& writeJson)
{
    int buyCoinsCount = 0;
    int size = readJson["coins"].size();
    int buyCoin = 0;
    bool isBuyCoin = false;
//    for(int i = 0; i < size; ++i)
//    {
//        Coin coin = readJson["coins"][0]["coin"];
//        int price = readJson["coins"][0]["price"];
//        buyCoinsCount = comparePrice(coin, price);
//        if(buyCoinsCount)
//        {
//            writeJson["buyCoins"][buyCoin]["coin"] = coin;
//            writeJson["buyCoins"][buyCoin]["count"] = isBuyCoin;
//            isBuyCoin = true;
//        }
//    }
    writeJson["buyCoins"][buyCoin]["coin"] = 1;
    writeJson["buyCoins"][buyCoin]["count"] = 10;
    InfoLog("readTimes:[%u]", readTimes);
//    return isBuyCoin;
    return true;
}

void StrategyService::readJson()
{
    int len = recv(sockFd, readMessage, MAX_READ_BUFF,0);
    epoll_event event;
    event.data.fd = sockFd;

    //这个判空很有必要，如果客户端结束，会收到消息，会一直触发epollin信号，但无内容
    //有的机器添加EPOLLRDHUP能正确识别epollin信号，但此机器不行
    if(len == 0)
    {
        InfoLog("close epollFd:[%u]", sockFd);
        epoll_ctl(epollFd, EPOLL_CTL_DEL, sockFd, &event);
        close(sockFd);
        return;
    }
    InfoLog("start recv message ");
    if(len < 0)
    {
        ErrorLog("read error");
        return;
    }
    InfoLog("recv msg lenth:[%u]", len);
    json readJson = json::parse(readMessage);
    std::cout << std::setw(4) << readJson << std::endl;
    json writeJson;
    if(readCoinsPrice(readJson, writeJson))
    {
        event.events = EPOLLOUT | EPOLLRDHUP | EPOLLONESHOT;
        epoll_ctl(epollFd, EPOLL_CTL_MOD, sockFd, &event);
        strcpy(writeMessage, writeJson.dump(4).c_str());
        std::cout << writeMessage << std::endl;
    }
    else
    {
        event.events = EPOLLIN | EPOLLRDHUP;
        epoll_ctl(epollFd, EPOLL_CTL_MOD, sockFd, &event);
    }
}

void StrategyService::writeJson()
{
    InfoLog("write fd:[%u]", sockFd);
    int len = write(sockFd, writeMessage, sizeof(writeMessage));
    InfoLog("send message lenth:[%u]", len);
    epoll_event event;
    event.data.fd = sockFd;
    event.events = EPOLLIN | EPOLLRDHUP | EPOLLONESHOT;
    event.data.fd = sockFd;
    epoll_ctl(epollFd, EPOLL_CTL_MOD, sockFd, &event);
    return;
}

